﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class MuonSach : DevComponents.DotNetBar.Controls.SlidePanel
    {
        DocGia _docGia = new DocGia();
        SqlDocGia _sqlDocGia = new SqlDocGia();

        Sach _sach = new Sach();
        SqlSach _sqlSach = new SqlSach();

        CuonSach _cuonSach = new CuonSach();
        SqlCuonSach _sqlCuonSach = new SqlCuonSach();

        PhieuMuonSach _phieuMuonSach = new PhieuMuonSach();
        SqlPhieuMuonSach _sqlPhieuMS = new SqlPhieuMuonSach();

        CTPhieuMuonSach _ctpMuonSach = new CTPhieuMuonSach();
        SqlCTPhieuMuonSach _sqlCtpMuonSach = new SqlCTPhieuMuonSach();        

        DataTable _tableSachMuon = new DataTable();

        public MuonSach()
        {
            InitializeComponent();
            createTableSachMuon();

            dgvSachMuon.DataSource = _tableSachMuon;
            dtimeNgayMuon.Value = DateTime.Today;
        }

        public void ShowSlide()
        {
            this.IsOpen = true;
            ResetField();
            UpdateSize();            
        }

        public void UpdateSize()
        {
            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
            grbThongTin.Location = new Point(
                this.Size.Width / 2 - grbThongTin.Size.Width / 2,
                this.Size.Height / 2 - grbThongTin.Size.Height / 2 - 20);
        }

        private void ResetField()
        {
            panel_Result.Text = "";
            txtMaDG.ReadOnly = false;
            txtMaCuonSach.ReadOnly = true;
            _tableSachMuon.Rows.Clear();
            txtMaDG.Text = null;
            txtMaCuonSach.Text = null;
            txtMaDG.Focus();
        }

        public void SetValue(int maDG)
        {
            txtMaDG.Text = maDG.ToString();
            getDocGia();
        }

        private void getDocGia()
        {
            if (txtMaDG.Text != "")
            {
                _docGia.MaDocGia = int.Parse(txtMaDG.Text);
                DataTable tableDG = _sqlDocGia.GetById(_docGia);
                if (tableDG.Rows.Count > 0)
                {
                    txtTenDG.Text = tableDG.Rows[0]["HoTen"].ToString();

                    DateTime date = (DateTime)tableDG.Rows[0]["NgaySinh"];
                    txtNgaySinh.Text = date.ToShortDateString();

                    date = (DateTime)tableDG.Rows[0]["NgayHetHan"];
                    txtHanThe.Text = date.ToShortDateString();

                    txtMaCuonSach.ReadOnly = false;
                    txtMaCuonSach.Focus();
                    txtMaDG.ReadOnly = true;
                }
                else
                {
                    MessageBox.Show("Mã độc giả không chính xác", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMaDG.Focus();
                    txtMaCuonSach.ReadOnly = true;
                }
            }
        }

        private void getSach()
        {
            if (txtMaCuonSach.Text != "")
            {
                _cuonSach.MaCuonSach = int.Parse(txtMaCuonSach.Text);
                DataTable table = _sqlCuonSach.GetInfoById(_cuonSach);
                if (table.Rows.Count > 0)
                {
                    txtTenSach.Text = table.Rows[0]["TenDauSach"].ToString();
                    txtTheLoai.Text = table.Rows[0]["TenTheLoai"].ToString();
                    txtTacGia.Text = table.Rows[0]["TenTacGia"].ToString();
                    if (table.Rows[0]["TinhTrang"].ToString() == "1")
                        txtTinhTrang.Text = "Chưa mượn";
                    else
                        txtTinhTrang.Text = "Đã mượn";
                    butMuon.Focus();
                }
                else
                {
                    MessageBox.Show("Mã cuốn sách không chính xác", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMaCuonSach.Focus();
                }
            }
        }

        private void createTableSachMuon()
        {
            // STT
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int16");
            column.ColumnName = "STT";
            _tableSachMuon.Columns.Add(column);

            // Mã Sách
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int16");
            column.ColumnName = "Mã Sách";
            _tableSachMuon.Columns.Add(column);

            // Tên Sách
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Tên Sách";
            _tableSachMuon.Columns.Add(column);

            // Thể loại
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Thể loại";
            _tableSachMuon.Columns.Add(column);

            // Tác giả
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Tác giả";
            _tableSachMuon.Columns.Add(column);

            // Tình trạng
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Tình trạng";
            _tableSachMuon.Columns.Add(column);
        }                        

        private bool checkInTable()
        {
            if (txtTenSach.Text == null)
            {
                panel_Result.Text = "Chưa nhập sách";
                return false;
            }
            if (txtTinhTrang.Text == "Đã mượn")
            {
                panel_Result.Text = "Sách đã được mượn";
                return false;
            }
            int ma = int.Parse(txtMaCuonSach.Text);
            foreach (DataRow row in _tableSachMuon.Rows)
            {
                if ((Int16)row["Mã Sách"] == ma)
                {
                    panel_Result.Text = "Sách đã có trong danh sách mượn";
                    return false;
                }
            }
            return true;
        }

        private void Nhap()
        {
            /************************************************************************/
            /* Phieu Muon Sach                                                      */
            /************************************************************************/
            _phieuMuonSach.MaDocGia = int.Parse(txtMaDG.Text);
            _phieuMuonSach.NgayMuon = dtimeNgayMuon.Text;
            _phieuMuonSach.SoSachMuon = _tableSachMuon.Rows.Count;
            _sqlPhieuMS.Insert(_phieuMuonSach);            
            //////////////////////////////////////////////////////////////////////////
            _ctpMuonSach.SoPM = _phieuMuonSach.SoPM;
            for (int i = 0; i < _tableSachMuon.Rows.Count; i++)
            {
                /************************************************************************/
                /* CT Phieu Muon Sach                                                   */
                /************************************************************************/
                _ctpMuonSach.MaCuonSach = (Int16)_tableSachMuon.Rows[i]["Mã Sách"];                
                _sqlCtpMuonSach.Insert(_ctpMuonSach);
                /************************************************************************/
                /* Cuon Sach                                                            */
                /************************************************************************/
                _cuonSach.MaCuonSach = (Int16)_tableSachMuon.Rows[i]["Mã Sách"];
                _sqlCuonSach.UpdateTinhTrang(_cuonSach);
            }
        }

        private void butMuon_Click(object sender, EventArgs e)
        {
            if (checkInTable())
            {
                DataRow row = _tableSachMuon.NewRow();
                row["Mã Sách"] = txtMaCuonSach.Text;
                row["Tên Sách"] = txtTenSach.Text;
                row["Thể loại"] = txtTheLoai.Text;
                row["Tác giả"] = txtTacGia.Text;
                row["Tình trạng"] = txtTinhTrang.Text;

                if (_tableSachMuon.Rows.Count > 0)
                    row["STT"] = (Int16)_tableSachMuon.Rows[_tableSachMuon.Rows.Count - 1]["STT"] + 1;
                else
                    row["STT"] = 1;
                _tableSachMuon.Rows.Add(row);
                txtMaCuonSach.Focus();
            }
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }

        private void txtMaCuonSach_Leave(object sender, EventArgs e)
        {
            getSach();            
        }

        private void txtMaCuonSach_TextChanged(object sender, EventArgs e)
        {            
            txtTenSach.Text = null;
            txtTheLoai.Text = null;
            txtTacGia.Text = null;
            txtTinhTrang.Text = null;

            panel_Result.Text = "";
        }

        private void txtMaDG_TextChanged(object sender, EventArgs e)
        {            
            txtTenDG.Text = null;
            txtNgaySinh.Text = null;
            txtHanThe.Text = null;           
        }

        private void txtMaDG_Leave(object sender, EventArgs e)
        {            
            getDocGia();            
        }

        private void butNhapLai_Click(object sender, EventArgs e)
        {
            ResetField();
        }

        private void txtMaDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMaDG_Leave(sender, e);
        }

        private void txtMaCuonSach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMaCuonSach_Leave(sender, e);
        }

        private void butNhap_Click(object sender, EventArgs e)
        {
            Nhap();
            panel_Result.Text = "Đã nhập thành công";
        }

        private void txtMaDG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }       
    }
}